1417. 重新格式化字符串
为保证权益,题目请参考 1417. 重新格式化字符串(From LeetCode).
解决方案1
Python
python
# 1417. 重新格式化字符串
# https://leetcode.cn/problems/reformat-the-string/
import string
from typing import List
class Solution:
def reformat(self, s: str) -> str:
alphas = []
nums = []
for st in s:
if st in string.digits:
nums.append(st)
else:
alphas.append(st)
if abs(len(alphas) - len(nums)) > 1:
return ""
def concat(a: List[str], b: List[str]) -> str:
if len(a) < len(b):
return concat(b, a)
ans = []
for i in range(min(len(a), len(b))):
ans.append(a[i])
ans.append(b[i])
if len(a) > len(b):
ans.append(a[-1])
return "".join(ans)
return concat(alphas, nums)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37